<template>
    <div class="search-page">
        <h1>搜索页面</h1>
        <div class="search-input" style="display: flex; align-items: center;">
            <input style="max-width: 500px; height: 100%;" type="text" v-model="searchQuery" placeholder="输入搜索内容..."
                @keyup.enter="performSearch" />
            <button @click="performSearch"
                style="margin-left: 10px;margin-top: -20px; padding: 10px 10px; background-color: #007bff; color: #fff; border: none; border-radius: 4px;  ">搜索商品</button>
        </div>
        <div class="search-results">
            <p v-if="searchResults.length === 0">没有找到相关结果。</p>
            <div class="item" @click="viewProduct(result)" v-for="result in searchResults" :key="result.productId"
                style="display: flex; align-items: center; cursor: pointer; margin-bottom: 20px; padding: 10px; border: 1px solid #ccc; border-radius: 4px; background-color: #fff;">
                <img :src="result.imageUrl" alt="商品图片"
                    style="max-width: 200px; height: 100px; object-fit: cover; margin-right: 20px;" />
                <div class="item-info" style="flex: 1;">
                    <h3 style="margin: 0 0 10px; font-size: 18px; color: #333;">{{ result.name }}</h3>
                    <p style="margin: 0; font-size: 14px; color: #666;">{{ result.sort }}</p>
                </div>
            </div>
        </div>
    </div>
</template>

<script setup>
import { ref, onMounted } from 'vue';
import { useRouter, useRoute } from 'vue-router';
import { get } from '../until/request';

const router = useRouter();  // 获取路由实例
const route = useRoute();    // 获取路由

const searchQuery = ref('');  // 搜索框内容
const searchResults = ref([]);  // 搜索结果

onMounted(() => {
    console.log("加载......");
    const key = route.query.key || ''; // 获取查询参数
    searchQuery.value = key;
    performSearch();
});
// 调用后端 API 进行搜索
async function performSearch() {
    if (searchQuery.value.trim()) {
        try {
            const response = await get('/api/search', { key: searchQuery.value });
            console.log(response);
            if (response && response.data) {
                searchResults.value = response.data;
            } else {
                searchResults.value = [];
            }
        } catch (error) {
            console.error('搜索失败:', error);
            searchResults.value = [];
        }
    } else {
        searchResults.value = [];
    }
}

// 查看商品详情
function viewProduct(product) {
    // 将产品信息存储到 localStorage
    localStorage.setItem('selectedProduct', JSON.stringify(product));
    router.push({ name: 'ProductDetail' });
}
</script>

<style scoped>
.search-page {
    padding: 20px;
    background-color: #f9f9f9;
}

input {
    width: 100%;
    padding: 10px;
    margin-bottom: 20px;
    border: 1px solid #ccc;
    border-radius: 4px;
}

.search-results {
    background-color: #fff;
    padding: 20px;
    border-radius: 4px;
    box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
}

ul {
    list-style: none;
    padding: 0;
}

li {
    padding: 10px 0;
    border-bottom: 1px solid #eee;
}

li:last-child {
    border-bottom: none;
}
</style>